package win.timloong.android.carwiki.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;

import com.squareup.picasso.OkHttpDownloader;
import com.squareup.picasso.Picasso;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import win.timloong.android.carwiki.util.OkHttpDownLoader;

/**
 * Created by tim on 16/7/11.
 */

public class CarWikiApplication extends Application {
    /**
     * 记录开启的Activity,便于关闭
     */
    private static List<Activity> activities = new ArrayList<Activity>();

    public static  Context context;
    @Override
    public void onCreate() {
        super.onCreate();

        context = getApplicationContext();

        OkHttpClient client = new OkHttpClient.Builder().
                cache(new Cache(new File(getExternalCacheDir().getAbsolutePath()),1024*1024*20))
                .build();

        //初始化图片缓存
        Picasso picasso = new Picasso.Builder(this).downloader(new OkHttpDownLoader(client)).build();
        Picasso.setSingletonInstance(picasso);
    }

    public static void addActivity(Activity activity) {


        activities.add(activity);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        for (Activity activity:activities) {
            activity.finish();
        }
    }


}
